package hutech.ksv.view;

import hutech.ksv.entity.ChuNhaTroEntity;
import hutech.ksv.entity.ChuQuanAnEntity;
import hutech.ksv.entity.CongTyTuyenDungEntity;
import hutech.ksv.entity.SinhVienEntity;
import hutech.ksv.entity.TaiKhoanCNTENtity;
import hutech.ksv.entity.TaiKhoanCQAEntity;
import hutech.ksv.entity.TaiKhoanCTEntity;
import hutech.ksv.entity.TaiKhoanSVEntity;
import hutech.ksv.service.EncryptMD5;
import hutech.ksv.service.UserAccountService;
import hutech.ksv.service.Utilities;

import java.io.Serializable;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.event.ValueChangeEvent;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class DangKyTaiKhoanManagedBean implements Serializable {

	/**
	 * 
	 */
	
	
	//Cac service
	@Autowired
	UserAccountService userService;
	
	@Autowired
	Utilities utilities;
	
	@Autowired
	EncryptMD5 encryptMD5;

	
	//Load trang lan dau tien
	@PostConstruct
	public void init() {
		this.isSinhVien = "show";
		this.isCongTy = "hide";
		this.thongTinChung = "show";
	}
	
	//On Load
	public void onLoad() {
		
	}
	
	public String dangKy() {
		
		boolean isTaiKhoanTonTai = false;
		
		if("sinhVien".equals(kieuThanhVien)) {
			SinhVienEntity sinhVienEntity = new SinhVienEntity();
			sinhVienEntity.setTenSV(hoTen);
			sinhVienEntity.setDiaChiSV(diaChi);
			
			//Convert 3 input string sang Date
			ngaySinh = utilities.convert3InputStringtoDate(ngaySinhNgay, ngaySinhThang, ngaySinhNam);
			
			sinhVienEntity.setNgaySinhSV(ngaySinh);
			sinhVienEntity.setTruongSV(truongSV);
			sinhVienEntity.setDiaChiTruongSV(diaChiTruong);
			sinhVienEntity.setMssv(mssv);
			sinhVienEntity.setSdtSV(sdt);
			sinhVienEntity.setGioiTinhSV(gioiTinh);
			sinhVienEntity.setEmailSV(email);			
			int maSV = userService.themSinhVien(sinhVienEntity);
			
			if(maSV > 0) {
				TaiKhoanSVEntity taiKhoanSV = new TaiKhoanSVEntity();
				taiKhoanSV.setTenTKSV(username);
				taiKhoanSV.setMatKhauTKSV(encryptMD5.md5(password));
				taiKhoanSV.setMaSV(maSV);
				
				isTaiKhoanTonTai = userService.kiemTraTaiKhoanDaTonTai(username);
				
				if(isTaiKhoanTonTai) {
					userService.xoaTaiKhoanSVVuaNhap(maSV);
					tinhTrangTaiKhoan = ("<span class='error-message'><b>Tài khoản đã tồn tại!</b></span>");
					return "DangKyThanhVien";
				} else {
					int result = userService.themTaiKhoanSV(taiKhoanSV);
					
					if(result > 0) {
						return "UserLogin";
					} else {
						return "DangKyTaiKhoanLoi";
					}
				}			
			}
			
		} else if("chuNhaTro".equals(kieuThanhVien)) {
			ChuNhaTroEntity chuNhaTroEntity = new ChuNhaTroEntity();
			chuNhaTroEntity.setTenCNT(hoTen);
			chuNhaTroEntity.setDiaChiCNT(diaChi);
			chuNhaTroEntity.setGioiTinhCNT(gioiTinh);
			chuNhaTroEntity.setSdtCNT(sdt);
			chuNhaTroEntity.setEmailCNT(email);
			
			int maCNT = userService.themChuNhaTro(chuNhaTroEntity);
			
			if(maCNT > 0) {
				TaiKhoanCNTENtity taiKhoanCNT = new TaiKhoanCNTENtity();
				taiKhoanCNT.setTenTKCNT(username);
				taiKhoanCNT.setMatKhauCNT(encryptMD5.md5(password));
				taiKhoanCNT.setMaCNT(maCNT);
								
				isTaiKhoanTonTai = userService.kiemTraTaiKhoanDaTonTai(username);
				
				if(isTaiKhoanTonTai) {
					userService.xoaTaiKhoanCNTVuaNhap(maCNT);
					tinhTrangTaiKhoan = ("<span class='error-message'><b>Tài khoản đã tồn tại!</b></span>");
					return "DangKyThanhVien";
				} else {
					int result = userService.themTaiKhoanCNT(taiKhoanCNT);
					
					if(result > 0) {
						return "UserLogin";
					} else {
						return "DangKyTaiKhoanLoi";
					}
				}	
			}
			
		} else if("chuQuanAn".equals(kieuThanhVien)) {
			ChuQuanAnEntity chuQuanAnEntity = new ChuQuanAnEntity();
			chuQuanAnEntity.setTenCQA(hoTen);
			chuQuanAnEntity.setDiaChiCQA(diaChi);
			chuQuanAnEntity.setGioiTinhCQA(gioiTinh);
			chuQuanAnEntity.setSdtCQA(sdt);
			chuQuanAnEntity.setEmailCQA(email);
			
			int maCQA = userService.themChuQuanAn(chuQuanAnEntity);
			
			if(maCQA > 0) {
				TaiKhoanCQAEntity taiKhoanCQA = new TaiKhoanCQAEntity();
				taiKhoanCQA.setTenTKCQA(username);
				taiKhoanCQA.setMatKhauCQA(encryptMD5.md5(password));
				taiKhoanCQA.setMaCQA(maCQA);				
				
				isTaiKhoanTonTai = userService.kiemTraTaiKhoanDaTonTai(username);
				
				if(isTaiKhoanTonTai) {
					userService.xoaTaiKhoanCQAVuaNhap(maCQA);
					tinhTrangTaiKhoan = ("<span class='error-message'><b>Tài khoản đã tồn tại!</b></span>");
					return "DangKyThanhVien";
				} else {
					int result = userService.themTaiKhoanCQA(taiKhoanCQA);
					
					if(result > 0) {
						return "UserLogin";
					} else {
						return "DangKyTaiKhoanLoi";
					}
				}
			}
			
		} else if("congTy".equals(kieuThanhVien)) {
			CongTyTuyenDungEntity congTyTuyenDungEntity = new CongTyTuyenDungEntity();
			congTyTuyenDungEntity.setTenCT(tenCongTy);
			congTyTuyenDungEntity.setDiaChiCT(diaChi);
			congTyTuyenDungEntity.setSdtCT(sdt);
			congTyTuyenDungEntity.setEmailCT(email);
			
			int maCT = userService.themCongTyTuyenDung(congTyTuyenDungEntity);
			
			if(maCT > 0) {
				TaiKhoanCTEntity taiKhoanCT = new TaiKhoanCTEntity();
				taiKhoanCT.setTenTKCT(username);
				taiKhoanCT.setMatKhauCT(encryptMD5.md5(password));
				taiKhoanCT.setMaCTTD(maCT);
				
				isTaiKhoanTonTai = userService.kiemTraTaiKhoanDaTonTai(username);
				
				if(isTaiKhoanTonTai) {
					userService.xoaTaiKhoanCTVuaNhap(maCT);
					tinhTrangTaiKhoan = ("<span class='error-message'><b>Tài khoản đã tồn tại!</b></span>");
					return "DangKyThanhVien";
				} else {
					int result = userService.themTaiKhoanCT(taiKhoanCT);
					
					if(result > 0) {
						return "UserLogin";
					} else {
						return "DangKyTaiKhoanLoi";
					}
				}
			}
		}
		return "DangKyTaiKhoanLoi";
	}
	
	public String cancel() {
		return "UserLogin";
	}
	
	public void hienThongTinSinhVien(ValueChangeEvent e) {
		if(e.getNewValue().toString().equals("sinhVien")) {
			isSinhVien = "show";
		} else {
			isSinhVien = "hide";
		}
		
		if(e.getNewValue().toString().equals("congTy")) {
			isCongTy = "show";
			thongTinChung = "hide";
		} else {
			isCongTy = "hide";
			thongTinChung = "show";
		}
	}
	
	public boolean checkValidEmail(String email) {
		pattern = Pattern.compile(email_pattern);
		matcher = pattern.matcher(email);
		return matcher.matches();

	}

	public boolean checkValidPhone(String sdt) {
		pattern = Pattern.compile(sdt_pattern);
		matcher = pattern.matcher(sdt);
		return matcher.matches();
	}
	
	//Khai bao cac property
	private static final long serialVersionUID = 1L;

	//Cac thong tin co ban
	private String hoTen;
	private String diaChi;
	private String sdt;
	private String gioiTinh;
	private String email;
	private String thongTinChung;
	
	//Ngay sinh
	private Date ngaySinh;
	private String ngaySinhNgay;
	private String ngaySinhThang;
	private String ngaySinhNam;
	
	//tai khoan
	private String username;
	private String password;
	
	//Neu la cong ty
	private String isCongTy;
	private String tenCongTy;
	
	//Hien cac thong tin can thiet cua sinh vien
	private String isSinhVien;
	private String truongSV;
	private String diaChiTruong;
	private String mssv;
	
	//Kieu thanh vien
	private String kieuThanhVien;
	
	//Kiem tra dinh dang cua du lieu nhap vao
	private final String email_pattern = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
	private final String sdt_pattern = "^\\s*\\d+\\s*$";
	private static Pattern pattern;
	private static Matcher matcher;
	
	//Bao loi neu nhap sai dinh dang
	private String tinhTrangEmail;
	private String tinhTrangSDT;
	
	//Tinh trang tai khoan (da ton tai hay chua)
	private String tinhTrangTaiKhoan;	
	
	public String getHoTen() {
		return hoTen;
	}
	public void setHoTen(String hoTen) {
		this.hoTen = hoTen;
	}
	public String getDiaChi() {
		return diaChi;
	}
	public void setDiaChi(String diaChi) {
		this.diaChi = diaChi;
	}
	public Date getNgaySinh() {
		return ngaySinh;
	}
	public void setNgaySinh(Date ngaySinh) {
		this.ngaySinh = ngaySinh;
	}
	public String getTruongSV() {
		return truongSV;
	}
	public void setTruongSV(String truongSV) {
		this.truongSV = truongSV;
	}
	public String getDiaChiTruong() {
		return diaChiTruong;
	}
	public void setDiaChiTruong(String diaChiTruong) {
		this.diaChiTruong = diaChiTruong;
	}
	public String getMssv() {
		return mssv;
	}
	public void setMssv(String mssv) {
		this.mssv = mssv;
	}
	public String getSdt() {
		return sdt;
	}
	public void setSdt(String sdt) {
		if (checkValidPhone(sdt)) {
			tinhTrangSDT = "";
		} else {
			tinhTrangSDT = ("<span class='error-message'><b>Số điện thoại sai định dạng!</b></span>");
		}
		this.sdt = sdt;
	}
	public String getGioiTinh() {
		return gioiTinh;
	}
	public void setGioiTinh(String gioiTinh) {
		this.gioiTinh = gioiTinh;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		if (checkValidEmail(email)) {
			tinhTrangEmail = "";
		} else {
			tinhTrangEmail = "<span class='error-message'><b>Email sai định dạng!</b></span>";
		}
		this.email = email;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	public String getIsSinhVien() {
		return isSinhVien;
	}
	public void setIsSinhVien(String isSinhVien) {
		this.isSinhVien = isSinhVien;
	}
	public String getKieuThanhVien() {
		return kieuThanhVien;
	}
	public void setKieuThanhVien(String kieuThanhVien) {
		this.kieuThanhVien = kieuThanhVien;
	}
	public String getNgaySinhNgay() {
		return ngaySinhNgay;
	}
	public void setNgaySinhNgay(String ngaySinhNgay) {
		this.ngaySinhNgay = ngaySinhNgay;
	}
	public String getNgaySinhThang() {
		return ngaySinhThang;
	}
	public void setNgaySinhThang(String ngaySinhThang) {
		this.ngaySinhThang = ngaySinhThang;
	}
	public String getNgaySinhNam() {
		return ngaySinhNam;
	}
	public void setNgaySinhNam(String ngaySinhNam) {
		this.ngaySinhNam = ngaySinhNam;
	}
	public String getTinhTrangEmail() {
		return tinhTrangEmail;
	}
	public void setTinhTrangEmail(String tinhTrangEmail) {
		this.tinhTrangEmail = tinhTrangEmail;
	}
	public String getTinhTrangSDT() {
		return tinhTrangSDT;
	}
	public void setTinhTrangSDT(String tinhTrangSDT) {
		this.tinhTrangSDT = tinhTrangSDT;
	}
	
	public String getIsCongTy() {
		return isCongTy;
	}
	public void setIsCongTy(String isCongTy) {
		this.isCongTy = isCongTy;
	}
	public String getTenCongTy() {
		return tenCongTy;
	}
	public void setTenCongTy(String tenCongTy) {
		this.tenCongTy = tenCongTy;
	}
	public String getThongTinChung() {
		return thongTinChung;
	}
	public void setThongTinChung(String thongTinChung) {
		this.thongTinChung = thongTinChung;
	}
	public String getTinhTrangTaiKhoan() {
		return tinhTrangTaiKhoan;
	}
	public void setTinhTrangTaiKhoan(String tinhTrangTaiKhoan) {
		this.tinhTrangTaiKhoan = tinhTrangTaiKhoan;
	}
}
